package com.example.asundae.particletest.boomview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import java.util.Random;

import static android.R.attr.radius;

/**
 * Created by super圣代 on 2017/6/11.
 */

public class Particle {

    float x;            ///粒子坐标
    float y;
    Rect bound;
    static Random random = new Random();
    float radius = FallingParticleFactory.PART_WH;
    float alpha = 1.0f;

    int color;          ///颜色值

    public Particle(float x, float y, Rect bound, int color) {
        this.x = x;
        this.y = y;
        this.bound = bound;
        this.color = color;
    }

    protected void caculate(float factor){
        x = x + factor * random.nextInt(bound.width()) * (random.nextFloat() - 0.5f);
        y = y + factor * random.nextInt(bound.height() / 2);

        radius = radius - factor * random.nextInt(2);

        alpha = (1f - factor) * (1 + random.nextFloat());
    }

    protected void draw(Canvas canvas,Paint paint){
        paint.setColor(color);
        paint.setAlpha((int) (Color.alpha(color) * alpha)); //这样透明颜色就不是黑色了
        canvas.drawCircle(x, y, radius, paint);
    }

    public void advance(Canvas canvas, Paint paint, float factor) {

        caculate(factor);

        draw(canvas,paint);

    }
}
